<?php 
include('common_include.php');
include('validateSession.php');

$prodCategory = $_POST['prodCategory'];
$prodSubcategory = $_POST['prodSubcategory'];
$prodName = $_POST['prodName'];
$prodId = $_POST['prodId'];
$prodCode = $_POST['prodCode'];
$prodDesc = $_POST['description'];
$unitPrice = $_POST['unitPrice'];
$postAllow = $_POST['postAllow'];
$postage = $_POST['postage'];
$sizeOpt = $_POST['sizeOpt'];
$colorOpt = $_POST['colorOpt'];
$formAction = $_POST['formAction'];
$error = "";
$successMsg = "";

$pageAction = $_GET['pageAction'];

if ($pageAction == "UpdInit") {
	$prodId = $_GET['prodId'];
	$dao = new SQLitePersistence();
	$aProduct = $dao->getProductsByProdId($prodId);
	$prodCode = $aProduct->getProductCode();
	$prodCategory = $aProduct->getCategory();
	$prodSubcategory = $aProduct->getSubcategory();
	$prodName = $aProduct->getProductName();
	$prodDesc = $aProduct->getDescription();
	$unitPrice = $aProduct->getUnitPrice();
	$postAllow = $aProduct->getPostAllow();
	$postage = $aProduct->getPostage();
	$sizeOpt = $aProduct->getSizeOpt();
	$colorOpt = $aProduct->getColorOpt();
	
	unset($dao);
	
} else if ($formAction == "Add" || $formAction == "Upd" || $formAction == "UpdInfoOnly") {
	$aProduct = new Product();
	$aProduct->setProductCode($prodCode);
	$aProduct->setCategory($prodCategory);
	$aProduct->setSubcategory($prodSubcategory);
	$aProduct->setProductName($prodName);
	$aProduct->setDescription($prodDesc);
	$aProduct->setUnitPrice($unitPrice);
	$aProduct->setPostAllow($postAllow);
	$aProduct->setPostage($postage);
	$aProduct->setSizeOpt($sizeOpt);
	$aProduct->setColorOpt($colorOpt);
	$aProduct->setDefunctInd("N");
	
	if ($formAction == "Add") {
		
		$dao = new SQLitePersistence();
		$result = $dao->saveNewProduct($aProduct);
		
		if ($result == 0) {
		
			$aProduct = $dao->getProductsByProdCode($prodCode);
			$prodId = $aProduct->getProductId();
			
			if ($_FILES["thumbnail"]["size"] > 0) {
			
				if ((($_FILES["thumbnail"]["type"] == "image/gif")
				|| ($_FILES["thumbnail"]["type"] == "image/jpeg")
				|| ($_FILES["thumbnail"]["type"] == "image/pjpeg"))
				&& ($_FILES["thumbnail"]["size"] < 2048000))
				  {
				  if ($_FILES["thumbnail"]["error"] > 0)
				    {
				    $error = "Error: " . $_FILES["thumbnail"]["error"] . "<br />";
				    }
				  else
				    {
				    $tmpFile = $_FILES["thumbnail"]["tmp_name"];
				    $newFileName = "thumbnail_" . $prodId . substr($_FILES["thumbnail"]["name"],strlen($_FILES["thumbnail"]["name"]) - 4,4);
				    $destFile = "images/thumbnail/" . $newFileName;
				    move_uploaded_file($tmpFile, $destFile);
				    $aProduct->setThumbnailPath($destFile);
				    }
				  }
				else
				  {
				  $error = "Record added without thumbnail file - Invalid thumbnail file, only gif/jpg file and size less than 2000KB ";
				  }
			}
			
			if ($_FILES["image1"]["size"] > 0) {
			  
				if ((($_FILES["image1"]["type"] == "image/gif")
				|| ($_FILES["image1"]["type"] == "image/jpeg")
				|| ($_FILES["image1"]["type"] == "image/pjpeg"))
				&& ($_FILES["image1"]["size"] < 2097152)) {
				  if ($_FILES["image1"]["error"] > 0) {
				    $error = "Error: " . $_FILES["image1"]["error"] . "<br />";
				  }
				  else {
				    $tmpFile = $_FILES["image1"]["tmp_name"];
				    $newFileName = "prodImg1_" . $prodId . substr($_FILES["image1"]["name"],strlen($_FILES["image1"]["name"]) - 4,4);
				    $destFile = "images/productImg/" . $newFileName;
				    move_uploaded_file($tmpFile, $destFile);
				    $aProduct->setImgPath1($destFile);
				  }
				}
				else {
				  $error = "Record added without product image file - Invalid image file, only gif/jpg file and size less than 2MB allowed ";
				}
			}
			
			if ($aProduct->getThumbnailPath() != "" || $aProduct->getImgPath1() != "") {
				$dao->updateProductImgPathById($aProduct);
			}
			 
			unset($dao);
			
			// Clean-up form after add record
			$prodCategory = "";
			$prodSubcategory = "";
			$prodName = "";
			$prodId = "";
			$prodCode = "";
			$prodDesc = "";
			$unitPrice = "";
			$postAllow = "";
			$postage = "";
			$sizeOpt = "";
			$colorOpt = "";
		} else if ($result == 1001) {
			$error = "Error: Add record failed! Product Code already exists in DB!";
		}
		$successMsg = "Success: Record added!";
	} else if ($formAction == "Upd" || $formAction == "UpdInfoOnly") {
		$prodId = $_POST['prodId'];
		$aProduct->setProductId($prodId);
		$dao = new SQLitePersistence();
		$dao->updateProductById($aProduct);
		
		if ($formAction == "Upd") {
		
			if ($_FILES["thumbnail"]["size"] > 0) {
					
				if ((($_FILES["thumbnail"]["type"] == "image/gif")
				|| ($_FILES["thumbnail"]["type"] == "image/jpeg")
				|| ($_FILES["thumbnail"]["type"] == "image/pjpeg"))
				&& ($_FILES["thumbnail"]["size"] < 2048000)) {
				  if ($_FILES["thumbnail"]["error"] > 0) {
				    $error = "Error: " . $_FILES["thumbnail"]["error"] . "<br />";
				  }
				  else {
				    $tmpFile = $_FILES["thumbnail"]["tmp_name"];
				    $newFileName = "thumbnail_" . $prodId . substr($_FILES["thumbnail"]["name"],strlen($_FILES["thumbnail"]["name"]) - 4,4);
				    $destFile = "images/thumbnail/" . $newFileName;
				    move_uploaded_file($tmpFile, $destFile);
				    $aProduct->setThumbnailPath($destFile);
				  }
				}
				else {
				  $error = "Record updated without thumbnail file - Invalid thumbnail file, only gif/jpg file and size less than 2000KB ";
				  $aProduct->setThumbnailPath("");
				}
			} else {
				 $aProduct->setThumbnailPath("");
			}
				
			if ($_FILES["image1"]["size"] > 0) {
			  
				if ((($_FILES["image1"]["type"] == "image/gif")
				|| ($_FILES["image1"]["type"] == "image/jpeg")
				|| ($_FILES["image1"]["type"] == "image/pjpeg"))
				&& ($_FILES["image1"]["size"] < 2097152)) {
				  if ($_FILES["image1"]["error"] > 0) {
				    $error = "Error: " . $_FILES["image1"]["error"] . "<br />";
				  }
				  else {
				    $tmpFile = $_FILES["image1"]["tmp_name"];
				    $newFileName = "prodImg1_" . $prodId . substr($_FILES["image1"]["name"],strlen($_FILES["image1"]["name"]) - 4,4);
				    $destFile = "images/productImg/" . $newFileName;
				    move_uploaded_file($tmpFile, $destFile);
				    $aProduct->setImgPath1($destFile);
				  }
				}
				else {
				  $error = "Record updated without product image file - Invalid image file, only gif/jpg file and size less than 2MB allowed ";
				  $aProduct->setImgPath1("");
				}
			} else {
				$aProduct->setImgPath1("");
			}
			
			$dao->updateProductImgPathById($aProduct);
			$successMsg = "Success: Record updated!";
		} else {
			$successMsg = "Success: Record updated without change of images!";
		}
		
		unset($dao);
		
		$formAction = UpdInit;
		
	}
	
}

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script language="javascript" src="scripts/common.js"></script>
<title>
	Browse Product
</title>
<script>

function clearProductForm() {
	document.updateProductForm.prodCategory.value="";
	document.updateProductForm.prodSubcategory.value="";
	document.updateProductForm.prodName.value="";
	document.updateProductForm.prodCode.value="";
	document.updateProductForm.description.value="";
	document.updateProductForm.unitPrice.value="";
	document.updateProductForm.postAllow[1] = true;
	document.updateProductForm.postage.value="";
	document.updateProductForm.sizeOpt.value="";
	document.updateProductForm.colorOpt.value="";
	document.updateProductForm.prodCategory.value="";
	clearFileInputField("divFileThumbnail");
	clearFileInputField("divFileImage1");
}

function clearFileInputField(tagId) {
    document.getElementById(tagId).innerHTML = document.getElementById(tagId).innerHTML;
}

function submitForAdd() {
	document.updateProductForm.action = "product_update.php";
	document.updateProductForm.formAction.value = "Add";
	if (validate()) {
		document.updateProductForm.submit();
	}
}

function submitForUpd() {
	document.updateProductForm.action = "product_update.php";
	document.updateProductForm.formAction.value = "Upd";
	if (validate()) {
		document.updateProductForm.submit();
	}
}

function submitForUpdProdInfo() {
	document.updateProductForm.action = "product_update.php";
	document.updateProductForm.formAction.value = "UpdInfoOnly";
	if (validate()) {
		document.updateProductForm.submit();
	}
}

function validate() {
	document.updateProductForm.prodCategory.value = trim(document.updateProductForm.prodCategory.value);
	document.updateProductForm.prodCode.value = trim(document.updateProductForm.prodCode.value);
	document.updateProductForm.prodName.value = trim(document.updateProductForm.prodName.value);
	document.updateProductForm.unitPrice.value = trim(document.updateProductForm.unitPrice.value);
	document.updateProductForm.postage.value = trim(document.updateProductForm.postage.value);
	document.updateProductForm.colorOpt.value = trim(document.updateProductForm.colorOpt.value);
	document.updateProductForm.sizeOpt.value = trim(document.updateProductForm.sizeOpt.value);
	
	if (document.updateProductForm.prodCategory.value == "") {
		alert("Product Category is required!");
		return false;
	} else if (document.updateProductForm.prodCode.value=="") {
		alert("Product Code is required!");
		return false;
	} else if (document.updateProductForm.prodName.value=="") {
		alert("Product Name is required!");
		return false;
	} else if (document.updateProductForm.description.value=="") {
		alert("Description is required!");
		return false;
	} else if (document.updateProductForm.unitPrice.value=="") {
		alert("Unit Price is required!");
		return false;
	} else if (document.updateProductForm.postAllow[0].checked && document.updateProductForm.postage.value=="") {
		alert("Postage is required!");
		return false;
	} else if (document.updateProductForm.colorOpt.value=="") {
		alert("Color Options is required!");
		return false;
	} else if (document.updateProductForm.sizeOpt.value=="") {
		alert("Size Options is required!");
		return false;
	}

	return true;
}

function bodyOnload() {
<?php
	if ($postAllow == "Y") {
?>
		document.updateProductForm.postAllow[0].checked = true;
<?php
	} else {
?>
		document.updateProductForm.postAllow[1].checked = true;
<?php
	}
?>
}

</script>
</head>
<body onload="bodyOnload();">
<form name="updateProductForm" method="post" enctype="multipart/form-data">
<input type="hidden" name="formAction" value="<?php echo $formAction?>">
<input type="hidden" name="prodId" value="<?php echo $prodId?>">
<table>
	<tr>
	<?php 
		if ($error) {
	?>
		<td class="errorLabel">
			<?php echo $error ?>
		</td>
	<?php 
		} else {
	?>
		<td class="successLabel">
			<?php echo $successMsg ?>
		</td>
	<?php 
		}
	?>
	</tr>
	<tr>
		<td class="labelClass">
			Add/Update Product:
		</td>
	</tr>
	<tr>
		<td>
		<table>
			<tr>
				<td class="textLabelClass">Product Category</td>
				<td class="textLabelClass">Subcategory</td>
				<td class="textLabelClass">Product Code</td>
			</tr>
			<tr>
				<td>
					<select name="prodCategory">
					  <option value="">-------- Please Select --------</option>
					  <?php
					  	if ($prodCategory == "Winter Accessories") {
					  ?>
					  		<option value="Winter Accessories" selected>Winter Accessories</option>
					  <?php
					  	} else {
					  ?>
					  		<option value="Winter Accessories">Winter Accessories</option>
					  <?php
					  	}
					  	
					  	if ($prodCategory == "Bags & Luggage") {
					  ?>
					  		<option value="Bags & Luggage" selected>Bags & Luggage</option>
					  <?php
					  	} else {
					  ?>
					  		<option value="Bags & Luggage">Bags & Luggage</option>
					  <?php
					  	}
					  	
					  	if ($prodCategory == "Casual Wear") {
					  ?>
					  		<option value="Casual Wear" selected>Casual Wear</option>
					  <?php
					  	} else {
					  ?>
					  		<option value="Casual Wear">Casual Wear</option>
					  <?php
					  	}
					  	
					  	if ($prodCategory == "Outdoor & Camping Products") {
					  ?>
					  		<option value="Outdoor & Camping Products" selected>Outdoor & Camping Products</option>
					  <?php
					  	} else {
					  ?>
					  		<option value="Outdoor & Camping Products">Outdoor & Camping Products</option>
					  <?php
					  	}
					  ?>
					</select>
				</td>
				<td>
					<input type="text" name="prodSubcategory" value="<?php echo $prodSubcategory?>" />
				</td>
				<td>
					<input type="text" name="prodCode" value="<?php echo $prodCode?>" />
				</td>
			</tr>
			<tr><td colspan="3">&nbsp;</td></tr>
			<tr>
				<td class="textLabelClass">Product Name</td>
				<td class="textLabelClass">Color Options</td>
				<td class="textLabelClass">Size Options</td>
			</tr>
			<tr>
				<td>
					<input type="text" name="prodName" value="<?php echo $prodName?>" />
				</td>
				<td>
					<input type="text" name="colorOpt" value="<?php echo $colorOpt?>" />
				</td>
				<td>
					<input type="text" name="sizeOpt" value="<?php echo $sizeOpt?>" />
				</td>
			</tr>
			<tr><td colspan="3">&nbsp;</td></tr>
			<tr>
				<td colspan="3" class="textLabelClass">Description</td>
			</tr>
			<tr>
				<td colspan="3">
					<textarea cols="25" rows="6" name="description"><?php echo $prodDesc?></textarea>
				</td>
			</tr>
			<tr><td colspan="3">&nbsp;</td></tr>
			<tr>
				<td class="textLabelClass">Unit Price</td>
				<td class="textLabelClass">Post Allow</td>
				<td class="textLabelClass">Postage</td>
			</tr>
			<tr>
				<td>
					<input type="text" name="unitPrice" value="<?php echo $unitPrice?>" />
				</td>
				<td valign="top">
					<!-- input type="text" name="postAllow" value="<?php echo $postAllow?>" /-->
					<input type="radio" name="postAllow" value="Y">Y
					&nbsp;&nbsp;
					<input type="radio" name="postAllow" value="N">N
				</td>
				<td valign="top">
					<input type="text" name="postage" value="<?php echo $postage?>" />
				</td>
			</tr>
			<tr><td colspan="3">&nbsp;</td></tr>
			<tr>
				<td class="textLabelClass">Thumbnail Upload</td>
				<td class="textLabelClass">&nbsp;</td>
				<td class="textLabelClass">&nbsp;</td>
			</tr>
			<tr>
				<td colspan="3">
				<div id="divFileThumbnail">
					<input type="file" name="thumbnail" id="thumbnail" />
				</div>
				</td>
			</tr>
			<tr><td colspan="3">&nbsp;</td></tr>
			<tr>
				<td class="textLabelClass">Image Upload</td>
				<td class="textLabelClass">&nbsp;</td>
				<td class="textLabelClass">&nbsp;</td>
			</tr>
			<tr>
				<td colspan="3">
				<div id="divFileImage1">
					<input type="file" name="image1" id="image1" />
				</div>
				</td>
			</tr>
			<tr>
				<td colspan="3">
				<?php 
					if ($formAction == "UpdInit") {
				?>
						<input type="button" name="btnUpd" value="Update Product Info Only" onclick="submitForUpdProdInfo();"/>
						&nbsp;&nbsp;
						<input type="button" name="btnUpd" value="Update Product Info & Image" onclick="submitForUpd();"/>
				<?php
					} else {
				?>
						<input type="button" name="btnAdd" value="Add" onclick="submitForAdd();"/>
						&nbsp;&nbsp;
						<input type="button" name="btnClear" value="Clear" onclick="clearProductForm();"/>
				<?php
					}
				?>
				</td>
			</tr>
		</table>
		</td>
	</tr>
	<tr><td>&nbsp;</td></tr>
	<tr><td>&nbsp;</td></tr>
</table>
</form>

</body>
</html>